package web.bean;

import core.entity.User;
import core.service.UserService;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import java.util.List;

/**
 * Created by Christian Waickem on 9/9/2014.
 */
@ManagedBean
@SessionScoped
public class UserBean {

    Long id = null;

    User user;

    List<User> userList;

    @ManagedProperty(value = "#{userService}")
    UserService userService;

    @ManagedProperty(value = "#{localeBean}")
    LocaleBean localeBean;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void initUserData(){
        userList = userService.getAllUsers();
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setLocaleBean(LocaleBean localeBean) {
        this.localeBean = localeBean;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String createUser(){
        userService.createUser(user);
        return "index?faces-redirect=true";
    }

    public void initUser(){
        if(this.id != null){
            user = userService.findUser(id);
        }
        else {
            user = new User();
        }
    }

    public void removeUser(User user){
        userService.removeUser(user);
        userList = userService.getAllUsers();
    }

    public String editUser(User user){
        this.user = user;
        return "edit?faces-redirect=true";
    }

    public String updateUser(){
        userService.updateUser(this.user);
        id = null;
        return "index?faces-redirect=true";
    }

    public String editUserById(long id){
        return "edit?id=" + id + "&faces-redirect=true";
    }

    public void findUser(long id){
        user = userService.findUser(id);
    }

}
